package com.suning.sawp.service.impl.bi;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.suning.sawp.constants.BiFileNameConstants;
import com.suning.sawp.constants.LoggerConstants;
import com.suning.sawp.dto.bi.BiDealErrorException;
import com.suning.sawp.intf.bi.BiDataTableRouteService;
import com.suning.sawp.intf.bi.BiFtpDataJobService;
import com.suning.sawp.intf.bi.BiFtpDataService;
import com.suning.sawp.service.util.DateUtils;

@Service("biFtpDataJobService")
public class BiFtpDataJobServiceImpl implements BiFtpDataJobService {

    private static final Logger LOGGER = LoggerFactory.getLogger(LoggerConstants.SAWP_BI);

    @Resource
    BiFtpDataService biFtpDataService;

    @Resource
    BiDataTableRouteService biDataTableRouteService;

    private void dealBiData(String biFileName, String dateStr) throws BiDealErrorException {
        // 是否需要向job平台抛异常，默认不需要
        boolean exceptionFlag = false;
        // 若未指定日期，则默认昨日日日期
        if (StringUtils.isBlank(dateStr)) {
            dateStr = DateUtils.getNextDayStr(-1, DateUtils.YYYYMMDD_PATTERN);
        } else {
            // 指定日期则是要重跑该日数据，先删除已有的数据
            biFtpDataService.deleteBiFtpDataByDay(biFileName, dateStr);
        }
        // 处理单个的文件
        try {
            biFtpDataService.dealBiFtpData(biFileName, dateStr);
        } catch (Exception e) {
            LOGGER.error("处理文件：{}，发生异常。", biFileName, e);
            exceptionFlag = true;
        }

        if (exceptionFlag) {
            throw new BiDealErrorException("解析BI数据: " + biFileName + "，发生异常，请留意。详情请查看错误报文记录。");
        }
    }

    @Override
    public void dealStoreManTotalIncome(String dateStr) throws BiDealErrorException {
        dealBiData(BiFileNameConstants.STORE_TOT_INC_D, dateStr);
    }

    @Override
    public void dealStoreManThisMonIncomeDetail(String dateStr) throws BiDealErrorException {
        dealBiData(BiFileNameConstants.STORE_AC_INC_DETAIL_D, dateStr);
    }

    @Override
    public void dealStoreManTotalDevMember(String dateStr) throws BiDealErrorException {
        dealBiData(BiFileNameConstants.STORE_TOT_MEMBER_D, dateStr);
    }

    @Override
    public void dealStoreManIncomeDetailRecover(String statisDate) throws BiDealErrorException {
        if (StringUtils.isBlank(statisDate)) {
            // 如果没有指定文件所处日期目录，则取上个月月末日期，与BI约定策略
            statisDate = DateUtils.getDateStrByFormat(DateUtils.getMaxMonthDate(null), DateUtils.YYYYMMDD_PATTERN);
        }
        biFtpDataService.dealBiIncDetailRecoverData(statisDate, BiFileNameConstants.STORE_AC_INC_DETAIL_D_RECOVER);
        // 更改重抛数据处理记录表的状态，上月置为处理过，同时新增本月未处理记录
        biDataTableRouteService.finishBiDataRecover();
    }

    @Override
    public void dealStoreManIncomeRecover(String statisDate) throws BiDealErrorException {
        // 取上个月最后一天日期
        String lastDayOfLastMonth = DateUtils.getDateStrByFormat(DateUtils.getMaxMonthDate(null),
                DateUtils.YYYYMMDD_PATTERN);
        if (StringUtils.isBlank(statisDate)) {
            // 如果没有指定文件所处日期目录，则取上个月月末日期，与BI约定策略
            statisDate = lastDayOfLastMonth;
        }
        // 先删除上个月最后一天的提成数据
        biFtpDataService.deleteBiFtpDataByDay(BiFileNameConstants.STORE_TOT_INC_D, lastDayOfLastMonth);
        // 处理重抛提成数据
        biFtpDataService.dealBiIncomeRecoverData(statisDate, BiFileNameConstants.STORE_TOT_INC_D_RECOVER);
    }

}
